using WeatherSystem.API.Models;

namespace WeatherSystem.API.Specifications
{
    /// <summary>
    /// 统计数据查询规范
    ///
    /// 提供多种统计数据查询方式，支持按城市、时间范围、统计类型等条件筛选
    /// 所有查询默认按统计日期降序排序
    /// </summary>
    public class StatisticsSpecification : BaseSpecification<StatisticsEntry>
    {
        /// <summary>
        /// 综合查询构造函数
        /// </summary>
        /// <param name="cityId">城市ID过滤</param>
        /// <param name="startDate">开始时间</param>
        /// <param name="endDate">结束时间</param>
        public StatisticsSpecification(Guid? cityId = null, DateTime? startDate = null, DateTime? endDate = null)
            : base(s =>
                (!cityId.HasValue || s.CityId == cityId.Value) &&
                (!startDate.HasValue || s.StatisticDate >= startDate.Value) &&
                (!endDate.HasValue || s.StatisticDate <= endDate.Value))
        {
            // 按统计日期降序排列
            ApplyOrderByDescending(s => s.StatisticDate);
        }

        /// <summary>
        /// 根据ID查询统计数据
        /// </summary>
        /// <param name="id">统计数据ID</param>
        public StatisticsSpecification(Guid id)
            : base(s => s.Id == id)
        {
        }

        /// <summary>
        /// 根据城市和统计类型查询统计数据
        /// </summary>
        /// <param name="cityId">城市ID</param>
        /// <param name="statisticType">统计类型</param>
        public StatisticsSpecification(Guid cityId, string statisticType)
            : base(s => s.CityId == cityId && s.StatisticType == statisticType)
        {
            // 按统计日期降序排列
            ApplyOrderByDescending(s => s.StatisticDate);
        }

        /// <summary>
        /// 根据城市、统计类型和时间范围查询统计数据
        /// </summary>
        /// <param name="cityId">城市ID</param>
        /// <param name="statisticType">统计类型</param>
        /// <param name="startDate">开始时间</param>
        /// <param name="endDate">结束时间</param>
        public StatisticsSpecification(Guid cityId, string statisticType, DateTime startDate, DateTime endDate)
            : base(s => s.CityId == cityId && s.StatisticType == statisticType && s.StatisticDate >= startDate && s.StatisticDate <= endDate)
        {
            // 按统计日期降序排列
            ApplyOrderByDescending(s => s.StatisticDate);
        }

        /// <summary>
        /// 自定义谓词查询构造函数
        /// </summary>
        /// <param name="predicate">自定义查询条件</param>
        public StatisticsSpecification(System.Linq.Expressions.Expression<Func<StatisticsEntry, bool>> predicate)
            : base(predicate)
        {
        }
    }
}